package com.taplinker.client;

import java.util.List;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.app.KeyguardManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Process;

import com.taplinker.client.core.image.ImageManager;
import com.taplinker.core.SimpleApplication;
import com.taplinker.core.util.CrashHandler;
import com.taplinker.sdk.core.MessageManager;

public class ClientApplication extends SimpleApplication {

	@Override
	public void onCreate() {
		super.onCreate();
		AppConfigFactory.getInstance().initAppConfig(getApplicationContext(),
				this);
//		MessageManager.getInstance().initialize(this);
		ImageManager.getInstance().initialize(getApplicationContext());

		CrashHandler crashHandler = CrashHandler.getInstance();
		crashHandler.init(this);
	}

	/**
	 * 后台运行
	 * 
	 * @return
	 */
	private boolean isBackgroundRunning() {
		ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
		KeyguardManager keyguardManager = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);

		if (activityManager == null) {
			return false;
		}
		// get running application processes
		List<ActivityManager.RunningAppProcessInfo> processList = activityManager
				.getRunningAppProcesses();
		for (ActivityManager.RunningAppProcessInfo process : processList) {
			if (process.pid == Process.myPid()) {
				boolean isBackground = process.importance != RunningAppProcessInfo.IMPORTANCE_FOREGROUND
						&& process.importance != RunningAppProcessInfo.IMPORTANCE_VISIBLE;
				boolean isLockedState = keyguardManager
						.inKeyguardRestrictedInputMode();
				if (isBackground || isLockedState)
					return true;
				else
					return false;
			}
		}
		return false;
	}

	public void notifyMessage(String message) {
		if (isBackgroundRunning()) {
			String notifySummary = message;
			String action = "com.taplinker.sdk.19581e27de7ced00ff1ce50b2047e7a567c76b1cbaebabe5ef03f7c3017bb5b7";

			NotificationManager notifyManager = (NotificationManager) AppConfigFactory
					.getInstance().getApplication()
					.getSystemService(Context.NOTIFICATION_SERVICE);
			Notification n = new Notification(AppConfigFactory.getInstance()
					.getApplication().getApplicationContext()
					.getApplicationInfo().icon, notifySummary,
					System.currentTimeMillis());
			Intent intent = new Intent(action);
			PendingIntent pi = PendingIntent.getBroadcast(AppConfigFactory
					.getInstance().getApplication(), (int) System
					.currentTimeMillis(), intent,
					PendingIntent.FLAG_UPDATE_CURRENT);
			n.setLatestEventInfo(AppConfigFactory.getInstance()
					.getApplication(), "您有新消息", notifySummary, pi);
			n.flags = Notification.FLAG_AUTO_CANCEL;
			n.contentIntent = pi;
			n.defaults = Notification.DEFAULT_SOUND;
			n.defaults = Notification.DEFAULT_VIBRATE;
			n.defaults = Notification.FLAG_SHOW_LIGHTS;

			notifyManager.notify((int) System.currentTimeMillis(), n);
		}
	}
}
